package application;
import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.JButton;

import connection.Connection;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import json.GsonImplements;


public class MongoDbApplicationFuncs {

	private JFrame frame;
	private JButton removeAllDocs;
	private JButton btnGson;
	private JButton uniqueKeyEnable;
	private JButton uniqueKeyDisable;
	private JButton removeRows;
	private JButton butFirst;
	private JButton showColletions;
	private JButton insertRow;
	private JButton showDataBases;
	private JButton removeOneRow;
	private JButton Exit;
	private JButton findRow;
	private Connection mongoDB;
	private JScrollPane scrollPane;
	private JTextArea textPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MongoDbApplicationFuncs window = new MongoDbApplicationFuncs();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MongoDbApplicationFuncs() {
		initialize();
		mongoDB = new Connection();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		frame = new JFrame();
		frame.setResizable(false);
		frame.setMinimumSize(new Dimension(732, 534));
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(10, 11, 514, 487);
		frame.getContentPane().add(scrollPane);
		
		textPane = new JTextArea();
		textPane.setEditable(false);
		scrollPane.setViewportView(textPane);
		
		butFirst = new JButton("Loading()");
		btnGson = new JButton("gSon()");
		showDataBases = new JButton("showDataBases()");
		showColletions = new JButton("showColletions()");
		insertRow = new JButton("insertRow()");
		findRow = new JButton("findRow()");
		removeOneRow = new JButton("removeOneRow()");
		removeRows = new JButton("removeRows()");
		removeAllDocs = new JButton("removeAllDocs()");
		uniqueKeyEnable = new JButton("uniqueKeyEnable()");
		uniqueKeyDisable = new JButton("uniqueKeyDisable()");
		Exit = new JButton("Exit()");
		
		btnGson.setBounds(534, 11, 182, 35);
		butFirst.setBounds(534, 51, 182, 35);
		showDataBases.setBounds(534, 130, 182, 35);
		showColletions.setBounds(534, 90, 182, 35);
		insertRow.setBounds(534, 170, 182, 35);
		findRow.setBounds(534, 210, 182, 35);
		removeOneRow.setBounds(534, 249, 182, 35);		
		removeRows.setBounds(534, 289, 182, 35);
		removeAllDocs.setBounds(534, 330, 182, 35);
		uniqueKeyEnable.setBounds(534, 370, 182, 35);
		uniqueKeyDisable.setBounds(534, 409, 182, 35);
		Exit.setBounds(534, 463, 182, 35);
		
		frame.getContentPane().add(butFirst);
		frame.getContentPane().add(btnGson);
		frame.getContentPane().add(showDataBases);
		frame.getContentPane().add(showColletions);
		frame.getContentPane().add(insertRow);
		frame.getContentPane().add(findRow);
		frame.getContentPane().add(removeOneRow);
		frame.getContentPane().add(removeRows);
		frame.getContentPane().add(removeAllDocs);
		frame.getContentPane().add(uniqueKeyEnable);
		frame.getContentPane().add(uniqueKeyDisable);
		frame.getContentPane().add(Exit);
		
		initActionListener();
	}

	public void initActionListener() {
		
		btnGson.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				GsonImplements.execute();
			}
		});
		
		showColletions.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Escrever(mongoDB.showColletions());
			}
		});
		
		showDataBases.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mongoDB.showDataBases();
			}
		});
		
		insertRow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mongoDB.insertRow();
			}
		});
		
		btnGson.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		butFirst.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});

		findRow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mongoDB.findRow();
			}
		});
		
		removeOneRow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mongoDB.removeOneRow();
			}
		});
		
		removeRows.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mongoDB.removeRows();
			}
		});
		
		removeAllDocs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mongoDB.removeAllDocs();
			}
		});
		
		uniqueKeyEnable.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mongoDB.uniqueKeyEnable();
			}
		});
		
		uniqueKeyDisable.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mongoDB.uniqueKeyDisable();
			}
		});
		
		Exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
	}

	protected void Escrever(String showColletions2) {
		for (String t: showColletions2.split("<@br>")){
			textPane.append(t);
			textPane.append("\n");
		}
	}
}
